package com.boom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.boom.config.Constants;
import com.boom.pojo.Role;
import com.boom.pojo.User;
import com.boom.service.RoleService;
import com.boom.service.UserService;
import com.mysql.jdbc.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @RequestMapping(value = "/list.html")
    public String getUserList(Model model, @RequestParam(value = "pageIndex", required = false) Integer currPageNo
            , @RequestParam(value = "queryname", required = false) String queryName
            , @RequestParam(value = "queryUserRole", required = false) Integer queryUserRole) {
        // 设置页面容量
        int pageSize = Constants.pageSize;
        //当前页码
        if (currPageNo == null) {
            currPageNo = 1;
        }
        if (queryUserRole == null) {
            queryUserRole = 0;
        }
        List<Role> roleList = roleService.getRoleList();
        IPage<User> iPageUser = userService.getUserList(queryName, queryUserRole, currPageNo, pageSize);
        List<User> userList = iPageUser.getRecords();

        int totalCount = (int) iPageUser.getTotal();
        //总页数
        int totalPageCount = (int) iPageUser.getPages();

        model.addAttribute("userList", userList);
        model.addAttribute("roleList", roleList);
        model.addAttribute("currentPageNo", currPageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("totalPageCount", totalPageCount);
        return "userlist";
    }

    @GetMapping("/view")
    @ResponseBody
    public Object view(@RequestParam String id){
        User user = new User();
        try {
            user = userService.getUserById(id);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return user;
    }

    @RequestMapping("/add.html")
    public String add(){
       // List<Role> list=roleService.getRoleList();
        return "useradd";
    }

    @RequestMapping("/ucexist.html")
    @ResponseBody
    public Object ucexist(@RequestParam String userCode){
        HashMap<String, String> resultMap = new HashMap<>();
        if (StringUtils.isNullOrEmpty(userCode)) {
            resultMap.put("userCode", "exist");
        }else {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("userCode", userCode);
            User user = userService.getUser(wrapper);
            if (user == null) {
                resultMap.put("userCode", "noexist");
            }else {
                resultMap.put("userCode", "exist");
            }
        }
        return resultMap;
    }

    @RequestMapping("/user.do")
    @ResponseBody
    public Object RoleName(){
        List<Role> list=roleService.getRoleList();
        return  list;
    }

    @RequestMapping(value = "/addsave.html",method = RequestMethod.POST)
    public String addsave(@Valid User user,HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("test/html;charset=utf-8");
        user.setCreationDate(new Date());
        user.setCreatedBy(1);
        int i=userService.insertUser(user);
        if (i>0){
            return "redirect:/user/list.html" ;
        }
        return "useradd";
    }

    @RequestMapping("/usermodify.html")
    public String usermodify(@RequestParam String uid,Model model){
        int id=Integer.parseInt(uid);
        User user=userService.getUserById(uid);
        model.addAttribute("user",user);
        return "usermodify";
    }

    @RequestMapping("/usermodifysave.html")
    public String usermodifysave(User user){
        int i=userService.updateUser(user);
        if (i>0){
            return "redirect:/user/list.html";
        }
        return "usermodify";
    }

    @RequestMapping("/deluser")
    @ResponseBody
    public Object deluser(@RequestParam String delId){
        Map<String,Object> map=new HashMap<>();
        if (delId.equals("")){
            map.put("delResult","notexist");
        }
        int id=Integer.parseInt(delId);
        int i=userService.deleteUser(id);
        if (i>0){
            map.put("delResult",true);
        }else
            map.put("delResult",false);
        return map;
    }
}
